package com.rshang.rsdp.service.impl;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.rshang.rsdp.annotation.Log;
import com.rshang.rsdp.bean.DataGrid;
import com.rshang.rsdp.bean.Response;
import com.rshang.rsdp.common.status.GsStatusCode;
import com.rshang.rsdp.common.status.StatusCode;
import com.rshang.rsdp.entity.DmRsdlGsa;
import com.rshang.rsdp.exception.RsdpSystemException;
import com.rshang.rsdp.kit.StrKits;
import com.rshang.rsdp.mapper.DmRsdlGsaMapper;
import com.rshang.rsdp.service.IDmRsdlGsaService;
import com.rshang.rsdp.util.i18n.I18nMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 公司代码表 服务实现类
 * </p>
 *
 * @author zhangjun
 * @since 2017-08-20
 */
@Service
public class DmRsdlGsaServiceImpl extends ServiceImpl<DmRsdlGsaMapper, DmRsdlGsa> implements IDmRsdlGsaService {

    @Autowired
    I18nMessage i18n;

    @Override
    public DataGrid getGsPageList(Page<HashMap> page, HttpServletRequest request) {
        String gsmc = StrKits.emptyToStr(request.getParameter("gsmc"));
        page.setRecords(this.baseMapper.getGsPageList(page, gsmc));
        return new DataGrid(page.getRecords(), page.getTotal());
    }

    @Log(module = "公司列表", desc = "新增公司")
    @Override
    public Response addSave(HttpServletRequest request, String czr) {
        if (this.baseMapper.selectCount(Condition.create().setSqlSelect("GS_ID")
                .eq("GS_MC", StrKits.emptyToStr(request.getParameter("gsmc")))) > 0) {
            throw new RsdpSystemException(GsStatusCode.GS_FOUND.getCode(), i18n.getMessage(GsStatusCode.GS_FOUND.getKey()));
        } else {
            DmRsdlGsa gs = new DmRsdlGsa();
            gs.setGsId(StrKits.uuid());
            gs.setGsMc(StrKits.emptyToStr(request.getParameter("gsmc")));
            gs.setLrr(czr);
            gs.setXgr(czr);
            gs.setYxBz(StrKits.emptyToStr(request.getParameter("sel_yxbz")));
            gs.setXyBz(StrKits.emptyToStr(request.getParameter("sel_xybz")));
            if (this.baseMapper.insert(gs) > 0) {
                return new Response(StatusCode.SUCCESS.getCode(), i18n.getMessage(StatusCode.SUCCESS.getKey()));
            } else {
                return new Response(StatusCode.OPER_FAILED.getCode(), i18n.getMessage(StatusCode.OPER_FAILED.getKey()));
            }
        }
    }

    @Log(module = "公司列表", desc = "公司编辑")
    @Override
    public Response editSave(HttpServletRequest request, String czr) {
        DmRsdlGsa gs = this.baseMapper.selectById(StrKits.emptyToStr(request.getParameter("hid_id")));
        if (gs == null) {
            throw new RsdpSystemException(GsStatusCode.GS_FOUND.getCode(), i18n.getMessage(GsStatusCode.GS_FOUND.getKey()));
        } else {
            gs.setGsId(StrKits.emptyToStr(request.getParameter("hid_id")));
            gs.setGsMc(StrKits.emptyToStr(request.getParameter("gsmc")));
            gs.setXgr(czr);
            gs.setXgsj(new Date());
            gs.setYxBz(StrKits.emptyToStr(request.getParameter("sel_yxbz")));
            gs.setXyBz(StrKits.emptyToStr(request.getParameter("sel_xybz")));
            if (this.baseMapper.updateAllColumnById(gs) > 0) {
                return new Response(StatusCode.SUCCESS.getCode(), i18n.getMessage(StatusCode.SUCCESS.getKey()));
            } else {
                return new Response(StatusCode.OPER_FAILED.getCode(), i18n.getMessage(StatusCode.OPER_FAILED.getKey()));
            }
        }
    }

    @Log(module = "公司列表", desc = "公司删除")
    @Override
    public Response deleteSave(HttpServletRequest request, String czr) {
        DmRsdlGsa result = this.baseMapper.selectById(StrKits.emptyToStr(request.getParameter("id")));
        if (result == null)//数据字典不存在
        {
            throw new RsdpSystemException(GsStatusCode.GS_NOTFOUND.getCode(), i18n.getMessage(GsStatusCode.GS_NOTFOUND.getKey()));
        } else        //物理删除
        {
            if (this.baseMapper.deleteById(result) > 0) {
                return new Response(StatusCode.SUCCESS.getCode(), i18n.getMessage(StatusCode.SUCCESS.getKey()));
            } else {
                return new Response(StatusCode.OPER_FAILED.getCode(), i18n.getMessage(StatusCode.OPER_FAILED.getKey()));
            }

        }
    }

    @Override
    public DmRsdlGsa get(HttpServletRequest request) {
        DmRsdlGsa result = this.selectById(StrKits.emptyToStr(request.getParameter("id")));
        if (result == null) {
            throw new RsdpSystemException(GsStatusCode.GS_NOTFOUND.getCode(), i18n.getMessage(GsStatusCode.GS_NOTFOUND.getKey()));
        } else {
            return result;
        }
    }

    @Override
    public Response checkGsmc(HttpServletRequest request) {
        String id = StrKits.emptyToStr(request.getParameter("id"));
        Wrapper wrapper = Condition.create().setSqlSelect("GS_ID")
                .eq("GS_MC", StrKits.emptyToStr(request.getParameter("gsmc")));

        if (!"".equals(id)) {
            wrapper = wrapper.ne("GS_ID", id);
        }
        if (this.baseMapper.selectCount(wrapper) > 0) {
            throw new RsdpSystemException(GsStatusCode.GS_FOUND.getCode(), i18n.getMessage(GsStatusCode.GS_FOUND.getKey()));
        }
        return new Response(StatusCode.SUCCESS.getCode(), i18n.getMessage(StatusCode.SUCCESS.getKey()));
    }

    @Override
    public List<DmRsdlGsa> getYxGs() {
        return this.baseMapper.selectList(Condition.create().setSqlSelect("GS_ID", "GS_MC")
                .eq("XY_BZ", "1")
                .eq("YX_BZ", "1")
                .orderBy("XGSJ", false));
    }

    @Override
    public List<DmRsdlGsa> getCxGs() {
        return this.baseMapper.selectList(Condition.create()
                .orderBy("XGSJ", false));
    }

    @Override
    public List<HashMap> getGsByCzr(String czr) {
        return this.baseMapper.getGsByCzr(czr);
    }

}
